package com.gitee.fanx.retty.demo.common;

import com.gitee.fanx.retty.exception.NoRouteFoundException;
import com.gitee.fanx.retty.spring.common.ResponseStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 统一异常处理 对于业务异常：返回头 Http 状态码一律使用500，避免浏览器缓存，在响应 Result 中指明异常的状态码 code
 *
 * @author fanhang
 */
@RestControllerAdvice
public class ExceptionResolver {

	@ResponseStatus
	@ExceptionHandler({ ServiceException.class })
	public WebResult<?> serviceException(final ServiceException e) {
		return WebResult.fail(e.getCode(), e.getMessage());
	}

	@ResponseStatus(404)
	@ExceptionHandler(NoRouteFoundException.class)
	public WebResult<?> routeNotFoundException(final NoRouteFoundException e) {
		return WebResult.fail("API [" + e.getRequest().path() + "] not existed");
	}

}
